package com.huadao.cart_ccs.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huadao.cart_ccs.R;

public class MultiStateTextView extends RelativeLayout {

    private TextView textView;
    private ImageView imageView;

    public final static int STATE_DEFAULT=0;
    public final static int STATE_UNKNOWN=1;
    public final static int STATE_OK=2;
    public final static int STATE_ERROR=3;

    private int state;
    private float textSize;
    private int textColor;
    private String text;
//    private Drawable drawable_default;
//    private Drawable drawable_unknown;
//    private Drawable drawable_ok;
//    private Drawable drawable_error;

    private int default_resId;
    private int unknown_resId;
    private int ok_resId;
    private int error_resId;

    public MultiStateTextView(Context context) {
        super(context);
    }

    public MultiStateTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.multistate_textview,this);
        textView=findViewById(R.id.textView);
        imageView=findViewById(R.id.imageView);

        initAttrs(context,attrs);
        applyAttrs();
    }

    public MultiStateTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
//        super(context, attrs, defStyleAttr);
        this(context, attrs);
    }

    private void initAttrs(Context context,AttributeSet attrs){
        TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.MultiStateTextView);
        state=array.getInt(R.styleable.MultiStateTextView_state,STATE_DEFAULT);
        textSize=array.getDimension(R.styleable.MultiStateTextView_textSize,getResources().getDimension(R.dimen.textSize_normal));
        textColor=array.getColor(R.styleable.MultiStateTextView_textColor,getResources().getColor(R.color.text_white));
        text=array.getString(R.styleable.MultiStateTextView_text);
//        if(array.hasValue(R.styleable.MultiStateTextView_imageDefault)) {
//            drawable_default = array.getDrawable(R.styleable.MultiStateTextView_imageDefault);
//        }
//        if(array.hasValue(R.styleable.MultiStateTextView_imageUnknown)) {
//            drawable_unknown = array.getDrawable(R.styleable.MultiStateTextView_imageUnknown);
//        }
//        if(array.hasValue(R.styleable.MultiStateTextView_imageOK)) {
//            drawable_ok = array.getDrawable(R.styleable.MultiStateTextView_imageOK);
//        }
//        if(array.hasValue(R.styleable.MultiStateTextView_imageERROR)) {
//            drawable_error = array.getDrawable(R.styleable.MultiStateTextView_imageERROR);
//        }

        if(array.hasValue(R.styleable.MultiStateTextView_imageDefault)) {
            default_resId = array.getResourceId(R.styleable.MultiStateTextView_imageDefault,R.drawable.check_default);
        }else {
            default_resId=R.drawable.check_default;
        }
        if(array.hasValue(R.styleable.MultiStateTextView_imageUnknown)) {
            unknown_resId = array.getResourceId(R.styleable.MultiStateTextView_imageUnknown,R.drawable.check_ing);
        }else {
            unknown_resId=R.drawable.check_ing;
        }
        if(array.hasValue(R.styleable.MultiStateTextView_imageOK)) {
            ok_resId = array.getResourceId(R.styleable.MultiStateTextView_imageOK,R.drawable.check_ok);
        }else {
            ok_resId=R.drawable.check_ok;
        }
        if(array.hasValue(R.styleable.MultiStateTextView_imageERROR)) {
            error_resId = array.getResourceId(R.styleable.MultiStateTextView_imageERROR,R.drawable.check_error);
        }else {
            error_resId=R.drawable.check_error;
        }
        array.recycle();
    }

    private void applyAttrs(){
        textView.getPaint().setTextSize(textSize);
        textView.setTextColor(textColor);
        if(text==null){
            text="";
        }
//        if(drawable_default==null){
//            drawable_default=getResources().getDrawable(R.mipmap.ic_launcher);
//        }
//        if(drawable_unknown==null){
//            drawable_unknown=getResources().getDrawable(R.mipmap.ic_launcher);
//        }
//        if(drawable_ok==null){
//            drawable_ok=getResources().getDrawable(R.mipmap.ic_launcher);
//        }
//        if(drawable_error==null){
//            drawable_error=getResources().getDrawable(R.mipmap.ic_launcher);
//        }
//        if(default_resId==0){
//            default_resId=R.mipmap.ic_launcher;
//        }
//        if(unknown_resId==0){
//            unknown_resId=R.mipmap.ic_launcher;
//        }
//        if(ok_resId==0){
//            ok_resId=R.mipmap.ic_launcher;
//        }
//        if(error_resId==0){
//            error_resId=R.mipmap.ic_launcher;
//        }
        textView.setText(text);
        setImageByState(state);
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
        setImageByState(state);
    }

    private void setImageByState(int state){
        switch (state){
//            case STATE_UNKNOWN:
//                imageView.setImageDrawable(drawable_unknown);
//                break;
//            case STATE_OK:
//                imageView.setImageDrawable(drawable_ok);
//                break;
//            case STATE_ERROR:
//                imageView.setImageDrawable(drawable_error);
//                break;
//            default:
//                imageView.setImageDrawable(drawable_default);
//                break;

            case STATE_DEFAULT:
                imageView.setImageResource(default_resId);
                break;
            case STATE_UNKNOWN:
                imageView.setImageResource(unknown_resId);
                break;
            case STATE_OK:
                imageView.setImageResource(ok_resId);
                break;
            case STATE_ERROR:
                imageView.setImageResource(error_resId);
                break;
            default:
                imageView.setImageResource(default_resId);
                break;
        }
    }
}
